package com.big.merchan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.big.cmn.DictFeignClient;
import com.big.common.utils.UUIDGenerator;
import com.big.merchan.mapper.MerchandiseCommentMapper;
import com.big.merchan.service.MerchandiseCommentService;
import com.big.merchan.service.MerchandiseReplyService;
import com.big.model.model.merchandise.comment.MerchandiseComment;
import com.big.model.model.merchandise.comment.MerchandiseReply;
import com.big.model.model.patient.PatientInfo;
import com.big.patient.PatientFeignClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class MerchandiseCommentServiceImpl extends ServiceImpl<MerchandiseCommentMapper,MerchandiseComment> implements MerchandiseCommentService {


    @Autowired
    MerchandiseReplyService merchandiseReplyService;


    @Autowired
    PatientFeignClient patientFeignClient;

    /**
     * 评论
     * @param mId
     * @return
     */
    @Override
    public IPage<MerchandiseComment> CommentInfos(Page<MerchandiseComment> pages, String mId) {
        QueryWrapper<MerchandiseComment>queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("m_id",mId);
        Page<MerchandiseComment> merchandiseCommentPage = baseMapper.selectPage(pages, queryWrapper);
        merchandiseCommentPage.getRecords().forEach(item -> {
            this.replys(item);
            this.patientInfo(item);
        });
        return merchandiseCommentPage;
    }

    //添加评论
    @Override
    public Integer saveMerchandiseComment(MerchandiseComment merchandiseComment) {
        merchandiseComment.setCId(Long.valueOf("411"+ UUIDGenerator.getUUID7()));
        return baseMapper.insert(merchandiseComment);
    }

    /**
     * 回复
     */
    public   MerchandiseComment replys(MerchandiseComment  comment){
        List<MerchandiseReply> merchandiseReplies = merchandiseReplyService.replyInfos(comment.getMId(),comment.getCId());
        comment.getParam().put("replies",merchandiseReplies);
        return  comment;
    }

    /**
     * 用户信息
     */

    public   MerchandiseComment patientInfo(MerchandiseComment  comment){
        PatientInfo userById = patientFeignClient.findUserById(String.valueOf(comment.getPId()));
        comment.getParam().put("patient",userById);
        return  comment;
    }




}
